package com.yann.bomb.crud;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yann.bomb.BaseActivity;
import com.yann.bomb.R;
import com.yann.bomb.crud.bean.Category;
import com.yann.bomb.util.LogUtils;
import com.yann.bomb.util.StringUtils;
import com.yann.bomb.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by yayun.xia on 2018/11/27
 */
public class DeleteActivity extends BaseActivity implements View.OnClickListener {

    private EditText edtDelete;
    private Button btnDel, btnBatchDel;
    private TextView tvLog;
    private String mObjectId;

    @Override
    public int intiLayout() {
        return R.layout.activity_delete;
    }

    @Override
    public void initView() {
        setTitle("删除Bomb测试");
        edtDelete = findView(R.id.edt_delete);
        edtDelete.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mObjectId = s.toString();
                if (StringUtils.isEmpty(mObjectId)) {
                    btnDel.setEnabled(false);
                    btnBatchDel.setEnabled(false);
                } else {
                    btnDel.setEnabled(true);
                    btnBatchDel.setEnabled(true);
                }
            }
        });
        btnDel = findView(R.id.btn_delete);
        btnDel.setOnClickListener(this);
        btnBatchDel = findView(R.id.btn_batch_delete);
        btnBatchDel.setOnClickListener(this);
        tvLog = findView(R.id.tv_log);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_delete:
                delete();
                break;
            case R.id.btn_batch_delete:
                deleteBatch();
                break;
            default:
                break;
        }
    }

    private void delete() {
        String mObjectId = getObjectIds()[0];
        Category category = new Category();
        category.delete(mObjectId, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    tvLog.setText("删除成功");
                    ToastUtils.showLong("删除成功");
                } else {
                    tvLog.setText("删除失败：" + e.getMessage());
                    ToastUtils.showLong("删除失败" + e.getMessage());
                }
            }
        });

    }

    private void deleteBatch() {
        List<BmobObject> categories = new ArrayList<>();
        for (int i = 0; i < getObjectIds().length; i++) {
            Category category = new Category();
            category.setObjectId(getObjectIds()[i]);
            categories.add(category);
        }
        new BmobBatch().deleteBatch(categories).doBatch(new QueryListListener<BatchResult>() {

            @Override
            public void done(List<BatchResult> results, BmobException e) {
                if (e == null) {
                    for (int i = 0; i < results.size(); i++) {
                        BatchResult result = results.get(i);
                        BmobException ex = result.getError();
                        if (ex == null) {
                            LogUtils.e("第" + i + "个数据批量删除成功：" + result.getCreatedAt() + "," + result.getObjectId() + "," + result.getUpdatedAt());
                        } else {
                            tvLog.setText("第" + i + "个数据批量删除失败：" + ex.getMessage() + "," + ex.getErrorCode());
                            LogUtils.e("第" + i + "个数据批量删除失败：" + ex.getMessage() + "," + ex.getErrorCode());
                        }
                        ToastUtils.showLong("删除成功");
                    }
                } else {
                    tvLog.setText("失败：" + e.getMessage() + "," + e.getErrorCode());
                    LogUtils.e("失败：" + e.getMessage() + "," + e.getErrorCode());
                    ToastUtils.showLong("删除失败：" + e.getMessage());
                }
            }
        });
    }

    public String[] getObjectIds() {
        String[] ObjectIds = mObjectId.split(",");
        for (int i = 0; i < ObjectIds.length; i++) {
            LogUtils.d("更新ObjectIds:" + ObjectIds[i]);
        }
        return ObjectIds;
    }
}
